package ac.svn.accesscontrol.controller.user;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JLabel;

import ac.notification.event.INotificationListener;
import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.context.Context;
import ac.svn.accesscontrol.context.IContextConstants;
import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.utils.Utility;

public class UserInfoEditorStatusController implements INotificationListener,ISVNIdentifier,IContextConstants
{

	private JLabel statusLabel = null;

	public UserInfoEditorStatusController(JLabel statusLabel)
	{
		this.statusLabel = statusLabel;
		NotificationRegistrar.registerListener(this);
	}

	public UserInfoEditorStatusController()
	{
		NotificationRegistrar.registerListener(this);
	}


	/**
	 * @return the statusLabel
	 */
	public JLabel getStatusLabel() {
		return statusLabel;
	}

	/**
	 * @param statusLabel the statusLabel to set
	 */
	public void setStatusLabel(JLabel statusLabel) {
		this.statusLabel = statusLabel;
	}

	//=====================================================//
	//														OVERRIDDEN SECTION													//
	//=====================================================//
	@Override
	public void receiveEvent(NotificationEvent notificationEvent) 
	{
		if(notificationEvent.getPropertyIdentifier().contains(N_USER_OPERATION_STATUS))
		{
			handleOperationStatus(notificationEvent);
		}
		else if(notificationEvent.getPropertyIdentifier().contains(N_GENERAL_MESSAGE_DISPLAY))
		{
			handleMessageDisplay(notificationEvent);
		}
	}

	private void handleMessageDisplay(NotificationEvent notificationEvent) 
	{
		String message = (String) notificationEvent.getPropertyValue();
		Integer messageTypeO = (Integer) notificationEvent.getParameter(KEY_MESSAGE_TYPE);
		if((null != messageTypeO) && (null != message))
		{
			statusLabel.setText(message);
			
			String icon = getIcon(messageTypeO.intValue());
			if(null != icon)
			{
				statusLabel.setIcon(Utility.getImageIcon(icon));
			}
		}
	}

	private void handleOperationStatus(NotificationEvent notificationEvent) 
	{
		//get context from Event
		Context context = (Context) notificationEvent.getPropertyValue();

		//Check if context is present at all.
		if(null == context){
			return ;
		}

		//Get Template of message to be displayed.
		String messageTemplate = getMessageTemplate(context);

		//Get operation type current being performed.
		String operationType = getOperationType(context);

		String finalText = String.format(messageTemplate, operationType);
		statusLabel.setText(finalText); 

		//Get operation state.
		Integer opStateO = (Integer) context.get(KEY_OPERATION_STATE);

		if(null != opStateO)
		{
			String iconPath = getIcon(opStateO.intValue());
			if(null != iconPath)
			{
				statusLabel.setIcon(Utility.getImageIcon(iconPath));
			}	
		}
	}

	private String getOperationType(Context context) 
	{
		String operationType = "";

		Integer opTypeO = (Integer) context.get(KEY_OPERATION_TYPE);

		if(null == opTypeO)
		{
			return operationType;
		}

		switch (opTypeO.intValue()) {
		case VALUE_OP_TYPE_ADDITION:
			operationType = "Addition";
			break;
		case VALUE_OP_TYPE_EDITING:
			operationType = "Editing";
			break;
		case VALUE_OP_TYPE_DELETION:
			operationType = "Deletion";
			break;
		case VALUE_OP_TYPE_UNDEFINED:
			operationType = "Undefined";
			break;
		default:
			break;
		}
		return operationType;
	}

	private String getMessageTemplate(Context context) 
	{
		String template = "";

		//Get operation state.
		Integer opStateO = (Integer) context.get(KEY_OPERATION_STATE);

		if(null == opStateO)
		{
			return template;
		}

		switch (opStateO.intValue()) 
		{
		case VALUE_OP_STATE_INPROGRESS:
			template = "Performing %s operation!";	
			break;
		case VALUE_OP_STATE_ABORTED:
			template = "%s operation has been aborted!";
			break;
		case VALUE_OP_STATE_COMPLETED:
			template = "%s operation has been completed!";
			break;
		case VALUE_OP_STATE_UNDEFINED:
			template = "Operation not defined!";
			break;
		default:
			break;
		}
		return template;
	}

	private String getIcon(int opState) 
	{
		String path = null;
		
		switch (opState) 
		{
		case VALUE_OP_STATE_INPROGRESS:
			path = IResources.LOC_ICN_INFORMATION;	
			break;
		case VALUE_OP_STATE_ABORTED:
			path = IResources.LOC_ICN_ERROR;
			break;
		case VALUE_OP_STATE_COMPLETED:
			path = IResources.LOC_ICN_DONE;
			break;
		case VALUE_OP_STATE_UNDEFINED:
			path = IResources.LOC_ICN_ERROR;
			break;
		default:
			path = IResources.LOC_ICN_ERROR;
			break;
		}
		return path;

	}

	@Override
	public Set<String> declaredSubscriptionSet() 
	{
		Set<String> subscriptionSet = new HashSet<String>();
		subscriptionSet.add(N_USER_OPERATION_STATUS);
		subscriptionSet.add(N_GENERAL_MESSAGE_DISPLAY);
		return subscriptionSet;
	}



}
